#====# Appendix F: Full disclosure of main text results #====#

# Load libraries and set defaults ----
library(modelsummary)
library(tinytable)
library(bizdays)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/event_function.R")

# prepare business calendar:
business_calendar <- create.calendar('biz_calendar', weekdays = c('saturday','sunday'))

# Import data ----
stocks <- read_rds("data_out/stocks_analysis.rds") # main analysis dataset

# Table F.1: The effect of Trump’s FCPA halt on firms’ AR and CAR ----
est <- return_daily_avg(data = stocks, abn_ret = "abn_chg", cum_abn_ret = "car", stratum = "FCPA_sample") %>%
  mutate(date = format(date, format = "%a, %b %d %Y")) %>%
  rename("term" = "date")

mod_ar1 <- list(tidy = est %>%
                  filter(FCPA_sample == "1" &
                           dv == "ar") %>%
                  select(-FCPA_sample, -dv),
                glance = est %>%
                  filter(FCPA_sample == "1" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car1 <- list(tidy = est %>%
                   filter(FCPA_sample == "1" &
                            dv == "car") %>%
                   select(-FCPA_sample, -dv),
                 glance = est %>%
                   filter(FCPA_sample == "1" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar0 <- list(tidy = est %>%
                  filter(FCPA_sample == "0" &
                           dv == "ar") %>%
                  select(-FCPA_sample, -dv),
                glance = est %>%
                  filter(FCPA_sample == "0" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car0 <- list(tidy = est %>%
                   filter(FCPA_sample == "0" &
                            dv == "car") %>%
                   select(-FCPA_sample, -dv),
                 glance = est %>%
                   filter(FCPA_sample == "0" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar_diff <- list(tidy = est %>%
                      filter(FCPA_sample == "diff" &
                               dv == "ar") %>%
                      select(-FCPA_sample, -dv),
                    glance = est %>%
                      filter(FCPA_sample == "diff" &
                               dv == "ar") %>%
                      filter(term == "Mon, Feb 10 2025") %>%
                      select(N) %>%
                      rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car_diff <- list(tidy = est %>%
                       filter(FCPA_sample == "diff" &
                                dv == "car") %>%
                       select(-FCPA_sample, -dv),
                     glance = est %>%
                       filter(FCPA_sample == "diff" &
                                dv == "car") %>%
                       filter(term == "Mon, Feb 10 2025") %>%
                       select(N) %>%
                       rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

modelsummary(list("(1) \\textsc{ar}" = mod_ar1,
                  "(2) \\textsc{car}" = mod_car1,
                  "(3) \\textsc{ar}" = mod_ar0,
                  "(4) \\textsc{car}" = mod_car0,
                  "(5) \\textsc{ar}" = mod_ar_diff,
                  "(6) \\textsc{car}" = mod_car_diff),
             # statistic = "[{conf.low}, {conf.high}]",
             notes = paste0("Average \\textsc{ar} and \\textsc{car} to past FCPA targets and matched placebo firms ",
                            "per day. Standard errors of the mean reported in parentheses. P-values from a two-tailed test ",
                            "of difference from zero for the average against a standard normal distribution. ",
                            "Estimation window starts 30 days and ends 5 days before FCPA Executive Order. Market models estimated using ",
                            "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                            "Columns 5 and 6 report the difference in means, respectively, between averages in columns 1 and 3, and those in columns 2 and 4.",
                            collapse = ""),
             title = "The effect of Trump's FCPA halt on firms' \\textsc{ar} and \\textsc{car} \\label{tab:results}",
             stars = c("*" = 0.05), 
             escape = FALSE) %>%
  group_tt(j = list("Past FCPA targets" = 2:3,
                    "Non-FCPA targets" = 4:5,
                    "Difference-in-means" = 6:7)) %>%
  group_tt(i = list("Pre-event:" = 1,
                    "Post-event:" = 11)) %>%
  style_tt(i = 21, line_color = "white", line_width = 0.1, line = "t") %>%
  style_tt(i = 22, line_color = "black", line_width = 0.05, line = "b") %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_F1.html", overwrite = TRUE)

# Table F.2: Heterogeneous effect on past FCPA targets by whether the investigation is still ongoing ----
est <- return_daily_avg(data = stocks %>%
                          filter(FCPA_sample == 1), 
                        abn_ret = "abn_chg", cum_abn_ret = "car", stratum = "ongoing") %>%
  mutate(date = format(date, format = "%a, %b %d %Y")) %>%
  rename("term" = "date")

mod_ar1 <- list(tidy = est %>%
                  filter(ongoing == "1" &
                           dv == "ar") %>%
                  select(-ongoing, -dv),
                glance = est %>%
                  filter(ongoing == "1" &
                           dv == "ar")%>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car1 <- list(tidy = est %>%
                   filter(ongoing == "1" &
                            dv == "car") %>%
                   select(-ongoing, -dv),
                 glance = est %>%
                   filter(ongoing == "1" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar0 <- list(tidy = est %>%
                  filter(ongoing == "0" &
                           dv == "ar") %>%
                  select(-ongoing, -dv),
                glance = est %>%
                  filter(ongoing == "0" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car0 <- list(tidy = est %>%
                   filter(ongoing == "0" &
                            dv == "car") %>%
                   select(-ongoing, -dv),
                 glance = est %>%
                   filter(ongoing == "0" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar_diff <- list(tidy = est %>%
                      filter(ongoing == "diff" &
                               dv == "ar") %>%
                      select(-ongoing, -dv),
                    glance = est %>%
                      filter(ongoing == "diff" &
                               dv == "ar") %>%
                      filter(term == "Mon, Feb 10 2025") %>%
                      select(N) %>%
                      rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car_diff <- list(tidy = est %>%
                       filter(ongoing == "diff" &
                                dv == "car") %>%
                       select(-ongoing, -dv),
                     glance = est %>%
                       filter(ongoing == "diff" &
                                dv == "car") %>%
                       filter(term == "Mon, Feb 10 2025") %>%
                       select(N) %>%
                       rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

modelsummary(list("(1) \\textsc{ar}" = mod_ar1,
                  "(2) \\textsc{car}" = mod_car1,
                  "(3) \\textsc{ar}" = mod_ar0,
                  "(4) \\textsc{car}" = mod_car0,
                  "(5) \\textsc{ar}" = mod_ar_diff,
                  "(6) \\textsc{car}" = mod_car_diff),
             # statistic = "[{conf.low}, {conf.high}]",
             notes = paste0("Average \\textsc{ar} and \\textsc{car} to past FCPA targets under ongoing investigations and not ",
                            "per day. Standard errors of the mean reported in parentheses. P-values from a two-tailed test ",
                            "of difference from zero for the average against a standard normal distribution. ",
                            "Estimation window starts 30 days and ends 5 days before FCPA Executive Order. Market models estimated using ",
                            "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                            "Columns 5 and 6 report the difference in means, respectively, between averages in columns 1 and 3, and those in columns 2 and 4.",
                            collapse = ""),
             title = "Heterogeneous effect on past FCPA targets by whether the investigation is still ongoing \\label{tab:results_ongoing}",
             stars = c("*" = 0.05), 
             escape = FALSE) %>%
  group_tt(j = list("Ongoing investigations" = 2:3,
                    "Non-ongoing investigations" = 4:5,
                    "Difference-in-means" = 6:7)) %>%
  group_tt(i = list("Pre-event:" = 1,
                    "Post-event:" = 11)) %>%
  style_tt(i = 21, line_color = "white", line_width = 0.1, line = "t") %>%
  style_tt(i = 22, line_color = "black", line_width = 0.05, line = "b") %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_F2.html", overwrite = TRUE)

#====# The End #====#